<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
need_login();

$current = 'diy_index';
$menu_current = 'diy_keyword';


/* 无应答时 */
if($_GET['zone'] == 'noanswer'){
	if( $_POST && $_POST['noanswer_text']){
		$noanswer_text_none = trim($_POST['noanswer_text']); //这是欢迎语的html格式
		$noanswer_text = preg_replace("/\s/","",$noanswer_text_none);
		$noanswer_text = str_replace('</p><p>', '\n\r', $noanswer_text);
		$noanswer_text = str_replace('<p>', '', $noanswer_text);
		$noanswer_text = str_replace('</p>', '', $noanswer_text);
		$noanswer_text = str_replace('<br />', '', $noanswer_text);
		$noanswer_text = str_replace('<br/>', '', $noanswer_text);
		$noanswer_text = str_replace('<p>', '', $noanswer_text);
		$noanswer_text = str_replace('&nbsp;', ' ', $noanswer_text); 
		$noanswer_text = str_replace('<spanid="__kindeditor_bookmark_end_2__"></span>', '', $noanswer_text);
		$noanswer_text = str_replace('<spanid="__kindeditor_bookmark_start_2__"></span>', '', $noanswer_text);
		$noanswer_text = str_replace('<atarget="_blank"', '<a ', $noanswer_text);
		$noanswer_text = str_replace('<ahref', '<a href', $noanswer_text); //这是欢迎语的文本格式
		//print_r($noanswer_text);exit;
		
		Table::UpdateCache('user', $login_user_id, array(
			'noanswer_text' => $noanswer_text, 
			'noanswer_text_none' => $noanswer_text_none, 
		));
		Session::Set('notice', '保存成功！');
		redirect( WEB_ROOT . "/diy/keyword.php?zone=noanswer");
	}
	
	include template('diy_keyword_noanswer');
}



/* 欢迎语 */
elseif($_GET['zone'] == 'welcome'){
	if( $_POST && $_POST['welcome_text']){
		$welcome_text_none = trim($_POST['welcome_text']); //这是欢迎语的html格式
		$welcome_text = preg_replace("/\s/","",$welcome_text_none);
		$welcome_text = str_replace('</p><p>', '\n\r', $welcome_text);
		$welcome_text = str_replace('<p>', '', $welcome_text);
		$welcome_text = str_replace('</p>', '', $welcome_text);
		$welcome_text = str_replace('<br />', '', $welcome_text);
		$welcome_text = str_replace('<br/>', '', $welcome_text);
		$welcome_text = str_replace('<p>', '', $welcome_text);
		$welcome_text = str_replace('&nbsp;', ' ', $welcome_text); 
		$welcome_text = str_replace('<spanid="__kindeditor_bookmark_end_2__"></span>', '', $welcome_text);
		$welcome_text = str_replace('<spanid="__kindeditor_bookmark_start_2__"></span>', '', $welcome_text);
		$welcome_text = str_replace('<atarget="_blank"', '<a ', $welcome_text);
		$welcome_text = str_replace('<ahref', '<a href', $welcome_text); //这是欢迎语的文本格式
		
		Table::UpdateCache('user', $login_user_id, array(
			'welcome_text' => $welcome_text, 
			'welcome_text_none' => $welcome_text_none, 
		));
		Session::Set('notice', '保存成功！');
		redirect( WEB_ROOT . "/diy/keyword.php?zone=welcome");
	}
	
	include template('diy_keyword_welcome');
}

/* 关键词应答 */
else{
	$condition = array(
		'user_id' => $login_user_id,
		'is_delete' => 0,
	);
	$count = Table::Count('keyword', $condition);
	list($pagesize, $offset, $pagestring) = pagestring($count, 20);
	$keywords = DB::LimitQuery('keyword', array(
		'condition' => $condition,
		'order' => 'ORDER BY id DESC',
		'size' => $pagesize,
		'offset' => $offset,
	));
	
	if($_POST['sub_keyword']){
		$keyword = $_POST['keyword'];
		$type = $_POST['type'];
		if('text' == $type){ //如果是文本形式的回答
			$text_content = $_POST['text_content'];
			$text_content_none = trim($_POST['text_content']); //这是欢迎语的html格式
			$text_content = preg_replace("/\s/","",$text_content_none);
			$text_content = str_replace('</p><p>', '\n\r', $text_content);
			$text_content = str_replace('<p>', '', $text_content);
			$text_content = str_replace('</p>', '', $text_content);
			$text_content = str_replace('<br />', '', $text_content);
			$text_content = str_replace('<br/>', '', $text_content);
			$text_content = str_replace('<p>', '', $text_content);
			$text_content = str_replace('&nbsp;', ' ', $text_content); 
			$text_content = str_replace('<spanid="__kindeditor_bookmark_end_2__"></span>', '', $text_content);
			$text_content = str_replace('<spanid="__kindeditor_bookmark_start_2__"></span>', '', $text_content);
			$text_content = str_replace('<atarget="_blank"', '<a ', $text_content);
			$text_content = str_replace('<ahref', '<a href', $text_content); //这是欢迎语的文本格式
			if($text_content && $text_content_none){
				$table = new Table('keyword', array(
					'keyword' => $keyword,
					'type' => $type,
					'user_id' => $login_user_id,
				));
				$insert = array('keyword', 'type', 'user_id');
				if ($flag = $table->update($insert)) {
					$keyword_id = $table->id;
					DB::Insert('response_text', array(
						'content' => $text_content,
						'content_none' => $text_content_none,
						'keyword_id' => $keyword_id,
					));
					Session::Set('notice', '新建成功！');
					die("<script>parent.location.reload();</script>");
				}
			}
		}elseif('news' == $type){ //如果是图文形式的回答
			$news_title = trim($_POST['news_title']);
			$news_content = trim($_POST['news_content']);
			if($news_title && $news_content){
				$table = new Table('keyword', array(
					'keyword' => $keyword,
					'type' => $type,
					'user_id' => $login_user_id,
				));
				$insert = array('keyword', 'type', 'user_id');
				if ($flag = $table->update($insert)) {
					$keyword_id = $table->id;
					$img = upload_image('news_img', null, 'team');
					DB::Insert('response_news', array(
						'title' => trim($_POST['news_title']),
						'img' => $img,
						'content' => $_POST['news_content'],
						'keyword_id' => $keyword_id,
						'url' => $_POST['news_url'],
					));
					Session::Set('notice', '新建成功！');
					die("<script>parent.location.reload();</script>");
				}
			}
		}elseif('newses' == $type){ //如果是多图文形式的回答
			
		}else{
			//nothing to do
		}
	}
	include template('diy_keyword');
}






















